package com.wf.it.hpwebplan.interceptor;

import com.wf.it.common.annotation.Login;
import com.wf.it.common.util.JsonUtil;
import com.wf.it.common.vo.Result;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod))
            return true;
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Login login=handlerMethod.getMethodAnnotation(Login.class);
        if(login==null)
            return true;

        Object user= request.getAttribute("cur_user");
        if(user==null){
            JsonUtil.outJson(response, Result.needLogin());
            return false;
        }
        return true;
    }
}
